<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查看资料</title>
<link type="text/css" href="${staticResources}/css/personalcenter.css" rel="stylesheet" />
<script src="${staticResources }/js/ajax.js" type="text/javascript"></script>
<script type="text/javascript">
var uploadFlag = "";

function uploadPortrait() {
	//选择文件
	var file1 = document.getElementById("file1");
	//file1.value = "";
	//file1.click();
	if (file1.value == "") return false;
	
	// TODO 判断文件类型
	
	//控件准备
	//document.getElementById("btnUpload").disabled = true;
	//document.getElementById("pUpload").style.visibility = "visible";
	
	//Ajax获取上传标记
	var params = "action=request_upload";
	doAjax("${contextPath }/Me/UploadPortrait", params, function(resp) {
		//返回值
		var rst = eval("(" + resp + ")");
		if (rst.status == 0 && rst.flag.length == 12) {
			uploadFlag = rst.flag;
			
			//提交表单
			document.getElementById("form1").action = "UploadPortrait?flag=" + uploadFlag;
			document.getElementById("form1").submit();
			
			//轮询上传进度
			setTimeout("checkStatus()", 1000);
		} else {
			alert("请求失败！");
			
			//document.getElementById("btnUpload").disabled = false;
			//document.getElementById("pUpload").style.visibility = "hidden";
		}
	});
}

var checkingStatusCount = 0;
function checkStatus() {
	//Ajax提交请求
	var params = "action=query_status";
	params += "&flag=" + uploadFlag;
	doAjax("${contextPath }/Me/UploadPortrait", params, function(resp) {
		//返回值
		var rst = eval("(" + resp + ")");
		if (rst.status == 0) {
			//document.getElementById("progressBar1").value = 100;
			//document.getElementById("txtProgress").innerHTML = "100";
			//document.getElementById("pUploadDetail").style.visibility = "hidden";
			//alert("上传完成！您可以继续上传新的图片。");
			//document.getElementById("file1").value = "";
			//document.getElementById("btnUpload").disabled = false;
			//location.href = location.href;
			parent.location.reload();
		} else if (rst.status == 1) {
			//正在上传
			checkingStatusCount = -1;
			//document.getElementById("progressBar1").value = rst.percentComplete;
			//document.getElementById("txtProgress").innerHTML = rst.percentComplete;
			//设置相关信息
			//document.getElementById("bytesProcessed").innerHTML = parseSize(rst.bytesProcessed);
			//document.getElementById("sizeTotal").innerHTML = parseSize(rst.sizeTotal);
			//document.getElementById("percentComplete").innerHTML = rst.percentComplete;
			//document.getElementById("uploadRate").innerHTML = parseSize(rst.uploadRate);
			//document.getElementById("timeInSeconds").innerHTML = rst.timeInSeconds;
			//document.getElementById("leftRuntime").innerHTML = rst.leftRuntime;
			//document.getElementById("pUploadDetail").style.visibility = "visible";
			setTimeout("checkStatus()", 1000);
		} else if (rst.status == 2) {
			//没有相关信息
			if (checkingStatusCount != -1)
				checkingStatusCount++;
			if (checkingStatusCount >= 20) {
				//上传超时
				alert("上传超时！请刷新网页然后重新上传！");
				return;
			}
			setTimeout("checkStatus()", 1000);
		} else if (rst.status == -3) {
			alert("数据库错误！");
		} else if (rst.status == -5) {
			alert("服务器错误！");
		} else {
			alert("错误代码：" + rst.status);
		}
	});
}
function parseSize(arg0) {
	return arg0;
}
</script>
</head>
<body onload="iframeResizePipe()">

<iframe id="helpframe" src='' height='0' width='0' frameborder='0'></iframe>
<script type="text/javascript">
  function iframeResizePipe()
  {
     // What's the page height?
     var height = document.body.scrollHeight;

     // Going to 'pipe' the data to the parent through the helpframe..
     var pipe = document.getElementById('helpframe');

     // Cachebuster a precaution here to stop browser caching interfering
     pipe.src = '${contextPath}/framehelper.html?height='+height+'&cacheb='+Math.random();
  }
</script>

<div class="con">
  <div class="uploadtx">
    <div class="grey2" style=" font-weight:600">上传头像</div>
    <div class="sctx">
      <div class="pic">
        <c:choose>
          <c:when test="${empty user.userPicture }">
            <img src="${staticResources}/image/userpics_none.jpg" width="100" height="100" alt=""><br />
          </c:when>
          <c:otherwise>
            <img src="${userContents}/userpics/${user.userPicture }" width="100" height="100" alt=""><br />
          </c:otherwise>
        </c:choose>
        <dl>当前头像</dl>
      </div>
      <div class="upload">
        <iframe id="target_upload" name="target_upload" src="" style="display: none"></iframe>
        <form id="form1" name="form1" enctype="multipart/form-data" method="post" target="target_upload">
          <label>请选择上传头像<input type="file" name="file1" id="file1" /></label>
          <div class="clear"></div>
          <div class="tijiao">
            <input type="image" style="" src="${staticResources}/image/submit.jpg" width="100" height="35"  class="button" name="" value="" onclick="uploadPortrait();return false;"/>
          </div>    
        </form>
      </div>
    </div>
  </div>
</div>

</body>
</html>